package com.j1903.contorller;

import com.j1903.annotation.PassJwt;
import com.j1903.util.JsonResult;
import com.j1903.util.QiNiuUtil;
import com.j1903.util.UploadRile;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
public class UpoadFileController {
    @RequestMapping("/img")
    @PassJwt
    public JsonResult uploadImg(@RequestParam("file")MultipartFile file){
//        String oriName=file.getOriginalFilename();
//        String extName=oriName.substring(oriName.lastIndexOf("."));
//        String newFieName=UUID.randomUUID().toString().replace("-", "");
//        String folder="d:\\springbootimages"+ File.separator
//                +newFieName;
//        String folder1="d:\\springbootimages"+ File.separator
//                + UUID.randomUUID().toString().replace("-", "")
//                +".jsp";
//        try {
//            file.transferTo(new File(folder));
//        }catch (Exception e)
//        {
//            e.printStackTrace();
//        }
//        String folder=request.getServletContext().getRealPath("images");
//        String fileName = UploadRile.saveFile(request, folder);
        String fildeName=null;
        try {
            fildeName=new QiNiuUtil().upload(file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  new JsonResult(200, "上传成功",fildeName);

    }

}
